package com.cnfangmao.masterdata.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.dto.EnumValueDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.EnumValue;
import com.cnfangmao.masterdata.service.EnumValueService;
import com.cnfangmao.masterdata.vo.EnumValueVo;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 字段枚举值 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Api(tags = "0-8、配置-枚举值管理")
@RestController
@RequestMapping("/enumValue")
public class EnumValueController {

	private static final Logger logger = LoggerFactory.getLogger(EnumValueController.class);

	@Autowired
	private EnumValueService enumValueService;

	/**
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "创建枚举值", notes = "创建枚举值")
	@ApiImplicitParam(name = "dto", value = "枚举值", required = true, paramType = "body", dataType = "EnumValueDto")
	@PostMapping("/create.do")
	public ResBody<String> create(@RequestBody EnumValueDto dto) {
		ResBody<String> resBody = new ResBody<String>();
		try {
			if (dto == null || dto.getTypeId() == null || dto.getValueCode() == null || dto.getValueName() == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，值为空");
				return resBody;
			}
			if (enumValueService.getEnumValueByCondition(dto.getTypeId(), dto.getValueCode(), dto.getValueName()) != null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("枚举值已经存在");
				return resBody;
			}

			// dto.setMappingId(SnowFlakeUtils.generator());
			EnumValue enumValue = ModelUtils.parseObject(dto, EnumValue.class, "create");
			enumValueService.save(enumValue);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("创建成功");
		} catch (Exception e) {
			logger.error(String.format("run create error in EnumValueController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;
	}

	/**
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "更新枚举值", notes = "更新枚举值")
	@ApiImplicitParam(name = "dto", value = "字段枚举类型", required = true, paramType = "body", dataType = "EnumValueDto")
	@PostMapping("/update.do")
	public ResBody<String> update(@RequestBody EnumValueDto dto) {
		ResBody<String> resBody = new ResBody<String>();
		try {
			if (dto == null || dto.getValueId() == null || dto.getTypeId() == null || dto.getValueCode() == null
					|| dto.getValueName() == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，值为空");
				return resBody;
			}

			EnumValue enumValue = ModelUtils.parseObject(dto, EnumValue.class, "update");
			enumValueService.updateById(enumValue);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("更新成功");
		} catch (Exception e) {
			logger.error(String.format("run update error in EnumValueController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;
	}

	/**
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "获取枚举值", notes = "获取枚举值")
	@ApiImplicitParam(name = "id", value = "枚举值的ID", required = true, paramType = "query", dataType = "string")
	@GetMapping("/getById.do")
	public ResBody<EnumValueVo> getById(@RequestParam(value = "id") String id) {
		ResBody<EnumValueVo> resBody = new ResBody<EnumValueVo>();
		try {
			if (id == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，枚举值的ID为空");
				return resBody;
			}
			EnumValueVo enumValue = enumValueService.getEnumValueById(id);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(enumValue);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run getById error in EnumValueController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除枚举值")
	@ApiImplicitParam(name = "ids", value = "枚举值的ID列表", required = true, allowMultiple = true, paramType = "query", dataType = "string")
	@PostMapping(value = "/delete.do")
	public ResBody<String> delete(@RequestParam(value = "ids") List<String> ids) {
		ResBody<String> resBody = new ResBody<String>();
		try {
			if (ids == null || ids.size() == 0) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，枚举值的ID为空");
				return resBody;
			}
			
			enumValueService.removeByIds(ids);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("删除成功");
		} catch (Exception e) {
			logger.error(String.format("run delete error in EnumValueController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	/**
	 * 
	 * @param searchDto
	 * @return
	 */
	@ApiOperation(value = "获取枚举值列表", notes = "获取枚举值列表")
	@ApiImplicitParam(name = "searchDto", value = "枚举值查询条件", required = true, paramType = "body", dataType = "GeneralSearchDto«EnumValueDto»")
	@PostMapping("/search.do")
	public ResBody<GeneralPageVo<EnumValueVo>> search(@RequestBody GeneralSearchDto<EnumValueDto> searchDto) {
		ResBody<GeneralPageVo<EnumValueVo>> resBody = new ResBody<GeneralPageVo<EnumValueVo>>();
		try {
			if (searchDto == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，值为空");
				return resBody;
			}
			
			GeneralPageVo<EnumValueVo> page = enumValueService.getEnumValuesByPage(searchDto);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(page);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run search error in EnumValueController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	/**
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取所有枚举值列表", notes = "获取所有枚举值列表")
	@GetMapping("/listAll.do")
	public ResBody<List<EnumValueVo>> listAll() {
		ResBody<List<EnumValueVo>> resBody = new ResBody<List<EnumValueVo>>();
		try {
			List<EnumValueVo> enumValues = enumValueService.getAllEnumValues();

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(enumValues);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run listAll error in EnumValueController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

}
